package com.example.testscalaandroid2.act

import org.scaloid.common.SActivity
import org.scaloid.common.SButton
import org.scaloid.common.SVerticalLayout
import org.scaloid.common.STextView
import org.scaloid.common.SIntent
import android.content.Intent
import org.scaloid.common.LocalServiceConnection
import org.scaloid.common.SLinearLayout
import android.graphics.drawable.ColorDrawable
import android.graphics.Color
import android.content.ComponentName
import android.os.IBinder
/*
 * One-time warning:
 * The Java to Scala conversion is nowhere near perfect (for various reasons).
 * The recommended way to use this feature is as the starting point for a conversion.
 * You should expect to heavily refactor the converted code, as well as review it for correctness.
 *
 * Report bugs to: https://github.com/dnadolny/java-to-scala-plugin
 */

//FIXME:scaloid需要更多的扩展
class ActAction extends SActivity {
  val player = new LocalServiceConnection[Service] {
    override def onServiceConnected(p1: ComponentName, b: IBinder) = {
      echo("onServiceConnected")
      super.onServiceConnected(p1, b)
    }
    override def onServiceDisconnected(p1: ComponentName) = {
      echo("onServiceDisconnected")
      super.onServiceDisconnected(p1)
    }
  }
  def echo(s: String) = {
    val ss = "ActAction " + s
    info(ss)
    toast(ss)
  }
  onCreate {
    echo("create")
    def pageChange(i: Int): Unit =
      contentView = new SVerticalLayout {
        style {
          case b: SButton => b.<<.wrap.>>
          case t: STextView => t.<<.wrap.>>
          case b: SLinearLayout => b
        }
        val hi = this
        this += new SLinearLayout {
          SButton("startAct", startActivity[TestAct])
          SButton("end", finish())
          SButton("alert", alert("??", "???", {}))
        }
        this += new SLinearLayout {
          STextView("no" + i)
          if (i > 0)
            SButton("上一页", pageChange(i - 1))
          SButton("下一页", pageChange(i + 1))
        }
        this += new SLinearLayout {
          SButton("resultAct", startActivityForResult((SIntent[TestAct2]).putExtra("name", "red"), 0))
          SButton("隐式打开", startActivity(new Intent("intent.IntAct")))
        }
        SButton("stratService", this += new SLinearLayout {

          SButton("open player", startService[Service])

          SButton("close ", stopService[Service])
          SButton("kill", hi.removeView(this))
        })

        SButton("bindService", this += new SLinearLayout {

          SButton("start ", player { p =>
            //            p.player.reset()
            p.player.start()
          })
          SButton("stop", player {
            _.player.stop
          })
          SButton("is playing", toast(player {
            _.player.isPlaying

          }.toString))
          SButton("isConn", toast(player.connected.toString))
          SButton("kill", hi.removeView(this))
        })

      }

    pageChange(0)
  }
  onDestroy {
    echo("onDestroy")
  }
  onPause {
    echo("onPause")
  }
  override def onRestart() = {
    echo("onRestart")
    super.onRestart()
  }
  onResume {
    echo("onResume")
  }
  onStart { echo("onStart") }
  onStop {
    echo("onStop")
  }
  override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) = { //code用于由哪里触发
    toast(s"name=${data.getStringExtra("name")}")
    super.onActivityResult(requestCode, resultCode, data)
  }

}

class TestAct extends SActivity {}
class TestAct2 extends SActivity {
  onCreate {
    contentView(new SVerticalLayout {
      val name = intent.map { _.getExtras().getString("name") }
      val nameTv = name.map { n =>
        STextView(s"name=$n")
      }
      SButton("close", {
        setResult(0, (new Intent).putExtra("name", name.getOrElse("") + "-")) //code用于由哪里触发
        finish
      })
    })
  }
}

class IntentAc extends SActivity {
  onCreate {
    contentView(new SVerticalLayout {
      STextView("隐式")
    })
  }
}